angular.module('myApp', ['infinite-scroll']);
//获得全部订单信息
function orderAll($scope, $http) {
	//初始化相关参数
	url = getUrl("26orderStatueSearch");
	header = getHeader();
	$scope.pages = 0;
	//默认加载第一页，初始值0
	$scope.orderList = [];
	$scope.totalpage = 1;
	//总页数初始默认1，读取数据后再更新
	//下滑加载
	$scope.loadMore = function() {
			if ($scope.pages < $scope.totalpage) {
				params = 'account=' + readData(localStorage.phone) + '&indexpage=' + $scope.pages + '&pageSize=4'
				$scope.pages = $scope.pages + 1;
				$http({
					method: 'POST',
					url: url,
					headers: header,
					timeout: 10000,
					//cache: true,
					data: params
				}).success(function(data, status, headers, config) {
					if (data.orderList) {
						//规格尺寸显示隐藏判断
						var produ = data.orderList;
						for (var i = 0; i < produ.length; i++) {
							for (var j = 0; j < produ[i].list.length; j++) {
								if (produ[i].list[j].speccolor) {
									produ[i].list[j].ngcolor = "false";
								} else {
									produ[i].list[j].ngcolor = "true";
								}
								if (produ[i].list[j].specsize) {
									produ[i].list[j].ngsize = "false";
								} else {
									produ[i].list[j].ngsize = "true";
								}
								if (produ[i].list[j].specspec) {
									produ[i].list[j].ngspec = "false";
								} else {
									produ[i].list[j].ngspec = "true";
								}
								if (produ[i].list[j].specmodel) {
									produ[i].list[j].ngmodel = "false";
								} else {
									produ[i].list[j].ngmodel = "true";
								}
							}
							if (produ[i].closedComment == "y") {
								produ[i].btn = false;
							} else {
								produ[i].btn = true;
							}
							produ[i].heji = (parseFloat(produ[i].amount) - parseFloat(produ[i].discountPay)).toFixed(2);
						}

						var orderList = produ;
						for (var i in orderList) {
							orderList[i]['cstatus'] = orderStatus(orderList[i]['status'], orderList[i]['isRefund']);
							//我加的 1订单评价 2付款 3确认收货 5删除订单
							orderList[i]['btnstatus'] = orderStatusCode(orderList[i]['status']);
							if (orderList[i]['isRefund'] == 'n') {
								/*if (orderList[i]['status'] == "noVerifi") {
								 orderList[i].isShow = false;
								 } else {
								 }*/
								orderList[i].isShow = true;
							} else {
								orderList[i].isShow = false;
							}
							$scope.orderList.push(orderList[i])
						}
						$scope.totalpage = data.totalpage;
						//下拉页面+1
					} else {
						$scope.pages = $scope.pages - 1;
						$scope.orderList = [];
					}
					//模拟状态数据
				}).error(function(data, status, headers, config) {
					$scope.pages = $scope.pages - 1;
					mui.toast('网络链接异常，请稍后重试(e)');
				});
			}
		}
		//联系导购
	$scope.openGuidechart = function(order) {
			delete localStorage.daogou;
			localStorage.daogou = writeData(JSON.stringify(order));
			localStorage.chatName = writeData('guidChat');
			mui.openWindow({
				url: '../chat/friendChat.html',
				id: 'friendChat.html'
			});
		}
		//申请退货
	$scope.tuihuo = function(order) {
			localStorage.order = writeData(JSON.stringify(order));
			api.openWin({
				name: 'tuihuo',
				url: 'tuihuo.html',
				reload: true
			});
		}
		//订单详情
	$scope.orderDetail = function(order) {
			localStorage.order = writeData(JSON.stringify(order));
			openOrderDetail();
		}
		//评价订单
	$scope.pj = function(order) {
			localStorage.order = writeData(JSON.stringify(order));
			openOrderJudge();
		}
		//支付订单
	$scope.pay = function(order) {
			localStorage.order = writeData(JSON.stringify(order));
			openOrderPay();
		}
		//确认收货
	$scope.check = function(order) {
			var orderId = order.orderId;
			urlc = getUrl("29receiveCheck");
			headerc = getHeader();
			mui.confirm('请确认收到商品后再提交', '确认收货', ['取消', '确认'], function(ret) {
				if (ret.index == 1) {
					plus.nativeUI.showWaiting("请稍等…");
					$http({
						method: 'POST',
						url: urlc,
						headers: headerc,
						timeout: 10000,
						//cache: true,
						//params: params
						data: 'id=' + orderId
					}).success(function(data, status, headers, config) {
						plus.nativeUI.closeWaiting();
						if (data.msg == 'success') {
							refreshOrderManager();
						} else if (data.code == "1") {
							mui.toast('您的订单还未发货')
						} else {
							mui.toast('网络异常，请稍后重试' + data.code)
						}

					}).error(function(data, status, headers, config) {
						plus.nativeUI.closeWaiting();
						mui.toast('网络异常，请稍后重试')
					});
				}
			})

		}
		//删除订单
	$scope.del = function(order) {
			var orderId = order.orderId;
			urlc = getUrl("30orderDelete");
			headerc = getHeader();
			mui.confirm('订单删除后将无法查看，您确定要删除？', '删除订单', ['取消', '确认'], function(ret) {

				if (ret.index == 1) {
					plus.nativeUI.showWaiting("请稍等…");
					$http({
						method: 'POST',
						url: urlc,
						headers: headerc,
						timeout: 10000,
						//cache: true,
						//params: params
						data: 'id=' + orderId
					}).success(function(data, status, headers, config) {
						plus.nativeUI.closeWaiting();
						if (data.msg == 'success') {
							var orderList = $scope.orderList;
							for (var i = 0; i < orderList.length; i++) {
								if (orderList[i]['orderId'] == orderId) {
									orderList.splice(i, 1)
								}
							}
							$scope.orderList = orderList;
							refreshOrderManager();
						} else {
							mui.toast('网络异常，请稍后重试' + data.code)
						};

					}).error(function(data, status, headers, config) {
						plus.nativeUI.closeWaiting()
						mui.toast('网络异常，请稍后重试')
					});
				}
			})

		}
		//取消订单
	$scope.cancle = function(order) {
		var orderId = order.orderId;
		urlc = getUrl("45orderCancle");
		headerc = getHeader();
		mui.confirm('确定要取消订单？', '取消订单', ['取消', '确认'], function(ret) {

			if (ret.index == 1) {
				plus.nativeUI.showWaiting("请稍等…")
				$http({
					method: 'POST',
					url: urlc,
					headers: headerc,
					timeout: 10000,
					//cache: true,
					//params: params
					data: 'id=' + orderId
				}).success(function(data, status, headers, config) {
					plus.nativeUI.closeWaiting();
					if (data.msg == 'success') {
						refreshOrderManager();
					} else {
						mui.toast('网络异常，请稍后重试' + data.code)
					};

				}).error(function(data, status, headers, config) {
					plus.nativeUI.closeWaiting();
					mui.toast('网络异常，请稍后重试')
				});
			}
		})

	}
}

function orderStatusCode(status) {
	var statusCode;
	switch (status) {
		case 'init':
			//未支付
			statusCode = 2;
			break;
		case 'pass':
			//支付  statusCode原来等于3 后来改为了0,0其实无意义
			statusCode = 0;
			break;
		case 'send':
			//发货
			statusCode = 3;
			break;
		case 'sign':
			//收货
			statusCode = 0;
			break;
		case 'cancel':
			//取消
			statusCode = 5;
			break;
		case 'file':
			//已完成
			statusCode = 1;
			break;
		case 'finish':
			//交易关闭
			statusCode = 5;
			break;
	}
	return statusCode
}
/*mui.init({
	pullRefresh: {
		container: '#pullrefresh',
		down: {
			contentdown: "下拉可以刷新",
			contentover: "释放立即刷新",
			contentrefresh: "正在刷新...",
			callback: pulldownRefresh
		},
	}
});

function pulldownRefresh() {
	alert(mui(".mui-content")[0].innerHTML)
	//window.location.reload();
	 mui('#refreshContainer').pullRefresh().endPulldownToRefresh();
}*/